<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">找回密码</h3>
      </div>
      <el-form-item prop="userName">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.userName" name="username" type="text" auto-complete="on" placeholder="用户名" />
      </el-form-item>
      <el-form-item prop="mobile">
        <div style="display: flex">
          <span class="svg-container svg-container_login" style="font-size: 22px;padding-left: 11px;">
            <svg-icon icon-class="mobile" />
          </span>
          <el-input v-model="loginForm.mobile" name="mobile" auto-complete="on" placeholder="手机号" @keyup.enter.native="handleLogin" />
          <el-button :loading="phoneloading" type="primary" :disabled="phoneDisabled" style="width: 130px" @click.stop="handlesend">{{phoneText}}</el-button>
        </div>
      </el-form-item>
      <el-form-item prop="checkSms">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input type="text" v-model="loginForm.checkSms" name="checkpic" auto-complete="on" placeholder="短信验证码" @keyup.enter.native="handleLogin" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :type="passwordType" v-model="loginForm.password" name="password" auto-complete="on" placeholder="密码" @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">提交</el-button>

    </el-form>

  </div>
</template>

<script>
  import { doGet } from '@/api/index'
  export default {
  name: 'ResetPassword',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('管理员密码长度应大于6'))
      } else {
        callback()
      }
    }
    const validateCheckSms = (rule, value, callback) => {
      if (value.length != 6) {
        callback(new Error('短信验证码必须为6位'))
      } else {
        callback()
      }
    }
    let mobileValidate = (rule, value, callback) => {
      let reg = /^[1][0-9]{10}$/;
      if (!reg.test(value)) {
        callback(new Error('手机号格式不正确'));
      } else {
        callback()
      }
    }
    return {
      checkpicSRC:"",
      loginForm: {
        userName: '',
        password: '',
        checkSms: '',
        mobile: ''
      },
      loginRules: {
        userName: [{ required: true, message: '管理员账户不允许为空', trigger: 'blur' }],
        password: [
          { required: true, message: '管理员密码不允许为空', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        checkSms: [
          { required: true, message: '短信验证码不允许为空', trigger: 'blur' },
          { validator: validateCheckSms, trigger: 'blur' }
        ],
        mobile: [{ required: true, message: '手机号不允许为空', trigger: 'blur' },
          { validator: mobileValidate, trigger: 'blur' }],
      },
      phoneloading: false,
      passwordType: 'password',
      loading: false,
      phoneDisabled: true,
      timer: null,
      phoneText: '获取验证码'
    }
  },
  watch: {
    loginForm: {
      handler: function(val) {
        let reg = /^[1][0-9]{10}$/;
        if (!reg.test(val.mobile)) {
          this.phoneDisabled = true
        } else {
          if (!this.timer) {
            this.phoneDisabled = false
          }
        }
      },
      deep: true
    }
  },
  created() {
    //
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
    clearTimeout(this.timer)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },

    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid && !this.loading) {
          this.loading = true
          doGet("login.html?op=resetpwd",this.loginForm).then((response)=>{
            this.loading = false
            this.$router.back()
          }).catch((error) => {
            this.$notify.error({
              title: '失败',
              message: error.message
            })
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    handlesend() {
      this.phoneloading = true
      doGet("sendsms.html",{phone: this.loginForm.mobile}).then((response)=>{
        this.phoneDisabled = true
        this.phoneloading = false
        this.setTime(10)
      }).catch((error)=>{
        this.$notify.error({
          title: '失败',
          message: error.message
        })
        this.phoneloading = false
      })
    },

    setTime(time) {
      if (time <= 0) {
        this.phoneText = '重新获取'
        this.phoneDisabled = false
        clearTimeout(this.timer)
        return
      }
      this.phoneText = `还剩${time}s`
      this.timer = setTimeout(() => {
        time--
        this.setTime(time)
      }, 1000)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd ,.show-forget {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .show-forget {
    &:hover {
      color: #FFFFFF;
      border-bottom: 1px solid #FFFFFF;
    }
  }
}
</style>
